var slice = [].slice;
var _toString = Object.prototype.toString;
// import CryptoJS from '@/static/libs/crypto-js';

export function isArray(obj) {
	return _toString.call(obj) === '[object Array]';
}

export function isString(val) {
	return typeof val === 'string';
}

export function isBoolean(val) {
	return val === true || val === false;
}

export function isFunction(val) {
	return typeof val === 'function';
}

export function isObject(obj) {
	return obj !== null && typeof obj === 'object';
}

export function isPlainObject(obj) {
	return _toString.call(obj) === '[object Object]';
}

export function forEach(obj, fn) {
	if (obj === null || typeof obj === 'undefined') {
		return;
	}

	if (typeof obj !== 'object') {
		obj = [obj];
	}

	if (isArray(obj)) {
		for (var i = 0, l = obj.length; i < l; i++) {
			fn.call(null, obj[i], i, obj);
		}
	} else {
		for (var key in obj) {
			if (Object.prototype.hasOwnProperty.call(obj, key)) {
				fn.call(null, obj[key], key, obj);
			}
		}
	}
}

export const assign = Object.assign || function(target) {
	slice.call(arguments, 1).forEach(source => {
		_merge(target, source);
	});
	return target;
};

export function merge(target) {
	slice.call(arguments, 1).forEach(source => {
		_merge(target, source, true);
	});
	return target;
}

function _merge(target, source, deep) {
	for (var key in source) {
		if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
			if (isPlainObject(source[key]) && !isPlainObject(target[key])) {
				target[key] = {};
			}
			if (isArray(source[key]) && !isArray(target[key])) {
				target[key] = [];
			}
			_merge(target[key], source[key], deep);
		} else if (source[key] !== undefined) {
			target[key] = source[key];
		}
	}
}

//解密方法
// export function decrypt(encryptedStr, key, iv) {
// 	try {
// 		let utf_key = CryptoJS.enc.Utf8.parse(key);
// 		let utf_iv = CryptoJS.enc.Utf8.parse(iv);
// 		let decrypt = CryptoJS.AES.decrypt(encryptedStr, utf_key, {
// 			iv: utf_iv,
// 			mode: CryptoJS.mode.CBC,
// 			padding: CryptoJS.pad.Pkcs7
// 		});
// 		let decryptedStr = CryptoJS.enc.Utf8.stringify(decrypt).toString();
// 		return decryptedStr;
// 	} catch (e) {
// 		console.log('解密错误！');
// 	}
// }
// export function encrypt(encryptedStr, key, iv) {
// 	let utf_key = CryptoJS.enc.Utf8.parse(key);
// 	let utf_iv = CryptoJS.enc.Utf8.parse(iv);
// 	let password = CryptoJS.enc.Utf8.parse(encryptedStr);

// 	let encrypted = CryptoJS.AES.encrypt(password, utf_key, {
// 		iv: utf_iv,
// 		mode: CryptoJS.mode.CBC,
// 		padding: CryptoJS.pad.Pkcs7
// 	});
// 	 decrypt(encrypted.toString(),key,iv);
// 	return encrypted.toString();	
// }

// export function randomIv (openid) {
// 	const time = Date.now();
// 	return CryptoJS.MD5(openid+time).toString().substr(8,16);
// }
